//
//  MainViewController.swift
//  SynWeibo
//
//  Created by 沈艳男 on 15/8/31.
//  Copyright © 2015年 沈艳男. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        addChildViewControllers()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
//        print(tabBar.subviews)
        setUpComposeButton()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // 按钮监听事件时有运行循环来调用，所以需要在运行循环注册，因此不能是用private
    // 必须在函数前加上@objc的关键字才能保证能在运行循环中调用
    // 添加按钮的点击事件
    @objc private func composedButtonClick() {
        
        print(__FUNCTION__)
    }
    
    // 添加按钮撰写，设置按钮的坐标
    private func setUpComposeButton(){
        // 获取按的个数
        let count = childViewControllers.count
        // 计算按的坐标，拿到其中一个按钮的宽度/按钮的个数
        let w = tabBar.bounds.width / CGFloat(count)
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        
        // 设置按钮的位置,把计算好的数据设置给按钮的frame
        composedButton.frame = CGRectOffset(rect, 2 * w, 0);
    }
    
    // MARK - 懒加载按钮控件
    private lazy var composedButton: UIButton = {
        
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(btn)
        
        
        // 添加按钮的点击事件
        btn.addTarget(self, action: "composedButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
    
    // 添加所有子控制器
    private func addChildViewControllers(){
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "信息",
            imageName: "tabbar_message_center")
        
        // 添加空控件
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "搜索", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    // 添加单独的子控制器
    private func addChildViewController(vc: UIViewController , title: String, imageName: String) {
        // 设置标题
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        // 创建导航控制器
        let nav = UINavigationController(rootViewController: vc)
        
        // 把导航控制器添加到控制器上
        addChildViewController(nav)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
